package com.example.rbac.uitls;

import com.example.rbac.config.httpClientConfig.HttpResult;
import com.google.common.collect.Lists;
import com.google.gson.*;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 模拟http请求
 */
@Component
public class HttpUtils {

    @Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;

    public String doGet(String url) throws Exception{
        HttpGet httpGet = new HttpGet(url);

        httpGet.setConfig(config);

        CloseableHttpResponse response = this.httpClient.execute(httpGet);

        if (response.getStatusLine().getStatusCode() == 200){
            return EntityUtils.toString(response.getEntity(), "utf-8");
        }
        return null;
    }


    public String doGet(String url, Map<String, Object> map)throws Exception{
        URIBuilder uriBuilder = new URIBuilder(url);
        if (map != null){
            for (Map.Entry<String, Object> entry : map.entrySet()){
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        return this.doGet(uriBuilder.build().toString());
    }

    public HttpResult doPost(String url, Map<String, Object> map)throws Exception{
        HttpPost httpPost = new HttpPost(url);

        httpPost.setConfig(config);

        if (map != null){
            List<NameValuePair> list = Lists.newArrayList();
            for (Map.Entry<String, Object> entry : map.entrySet()){
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");

            httpPost.setEntity(urlEncodedFormEntity);
        }

        CloseableHttpResponse response = this.httpClient.execute(httpPost);
        return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(
                response.getEntity(), "UTF-8"));
    }


    public HttpResult doPost(String url)throws Exception{
        return this.doPost(url, null);
    }


    public JsonObject toJsonObj(String str){
        JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
        return jsonObject;
    }

    public JsonArray toJsonArray(String str){
        JsonArray jsonArray = new JsonParser().parse(str).getAsJsonArray();
        return jsonArray;
    }
}
